﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VerficationsWPF.Models
{
    /// <summary>
    /// DependencyPropertyModel.xaml 的交互逻辑
    /// </summary>
    public partial class DependencyPropertyModel : BaseModel
    {
        Student stu = new Student();

        public DependencyPropertyModel()
        {
            InitializeComponent();
            Title = "Dependency Property Demo";
            Description = "将Student的实例与前面的两个TextBox关联，同时使用IValueConverter进行数据转换。";

            // 1. 直接绑定
            var nameBindingObject = new Binding("Name");
            nameBindingObject.Mode = BindingMode.TwoWay;
            nameBindingObject.Source = stu;
            nameBindingObject.Converter = new StringConverter();
            // nameBindingObject.ConverterCulture = new CultureInfo("en-US");
            BindingOperations.SetBinding(tb1, TextBox.TextProperty, nameBindingObject);
            BindingOperations.SetBinding(tb2, TextBox.TextProperty, nameBindingObject);

            // 2. 封装绑定
            //stu.SetBinding(Student.NameProperty, new Binding("Text") { Source = tb1 });
            //tb1.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = stu });
        }



        private void btnBind_Click(object sender, RoutedEventArgs e)
        {
            stu.Name = "tom";
            tb1.Text = stu.Name;
        }

        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine(stu.Name);
        }
    }

    class StringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object res = "";
            if (value is string str)
            {
                res = "Name=" + str;
            }
            return res;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object res = "";
            if (value is string str && str.StartsWith("Name="))
            {
                res = str.Substring(5);
            }
            return res;
        }
    }
    /// <summary>
    /// 基于WPF依赖属性的学生类。
    /// </summary>
    class Student : DependencyObject
    {
        /// <summary>
        /// Student Name.
        /// </summary>
        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof(string), typeof(Student), new PropertyMetadata("noname"));

        /// <summary>
        /// 添加数据绑定。
        /// </summary>
        /// <param name="target_dp">外部对象要绑定的依赖属性。</param>
        /// <param name="binding"></param>
        /// <returns></returns>
        public BindingExpressionBase SetBinding(DependencyProperty target_dp, BindingBase binding)
        {
            return BindingOperations.SetBinding(this, target_dp, binding);
        }

    }
}
